<%@page import="domain.BlogFile"%>
<%@page pageEncoding="UTF-8" %>
<%@page import="utils.DateUtils"%>
<%@page import="getools4me.BlogManager"%>
<%@page import="getools4me.UserManager"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%
	UserService us = UserServiceFactory.getUserService();
	User user = us.getCurrentUser();
	String thisUrl = request.getRequestURI();
	String loginURL = us.createLoginURL(thisUrl);
	String logoutURL = us.createLogoutURL(thisUrl);
	String blogID = request.getParameter("blogID");
%>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
/**
 * Common, contains some common functions
 */

var Common = function(){
	
}

// convert the text into unicode, 4 digits for one single character
Common.getUnicodeFromString = function(str){
	var result="";
	for(i=0;i<str.length;i++){
		var unicode = "0000" + str.charCodeAt(i).toString(16).toUpperCase();
		unicode = unicode.substring(unicode.length-4);
		result = result + unicode;			
	}
	return result;
}

// convert a sequence of unicode(hex) to a string
Common.getStringFromUnicode = function(unicode){
	var index=0;
	var unit = unicode.substr(index*4,4);
	var result = "";
	while(unit.length==4){
		result = result + String.fromCharCode(parseInt(unit, 16));
		index++;
		unit = unicode.substr(index*4,4);
	}
	return result;
}
	

// show hidden out, by converting unicode to string
Common.prototype.convertUnicodeClassElementToTargetElement = function(document){
	var list = document.getElementsByClassName("unicode");
	var i=0;
	for(i=0;i<list.length;i++){
		document.getElementById(this.getTargetID(list[i].id)).value = Common.getStringFromUnicode(list[i].value);
	}
}
	
Common.prototype.convertStringClassElementToTargetElement = function(document){
	var list = document.getElementsByClassName("string");
	var i=0;
	for(i=0;i<list.length;i++){
		document.getElementById(this.getTargetID(list[i].id)).value = Common.getUnicodeFromString(list[i].value);
	}
}


// id convert
Common.prototype.getTargetID = function(id){
	return id+".target";
}
</script>

<style type="text/css">

div.export{
	width: 60%;
	margin-left: 100px;
	border-top: 1px solid;
}

div.export div.upload input[type="text"]{
	width: 80%;
}

div.file {
	border-bottom: 1px solid;
	margin-top: 10px;
	padding-bottom: 10px;
}

div.file label{
	font-weight: bold;
	float: left;
	width: 150px;
	text-align: right;
	margin-right: 5px;
}

div.file input[type="text"]{
	border:none;
}

div.file textarea{
	border: none;
	resize:none;
}

div.file br{
	clear:left;
}

.unicode{
	display: none;
}

</style>

<title>Export_<%=request.getServerName()%>_<%=DateUtils.format("yyyy-MM-dd_HH-mm-ss",System.currentTimeMillis()) %></title>
</head>
<body>




<%if(user==null){ %> 
	<div class="center">
		<h1><a href="<%=loginURL%>">Please Login ...</a></h1>
	</div>
<%}else if(!user.getEmail().equals("jpgtama@gmail.com")) { %>
	<div class="center">
		<h1>Welcome, Mr. <%=user.getNickname() %> !</h1>
		<i>Sorry, you are not admin.(<a href="<%=logoutURL%>">Logout</a>)    </i>
	</div>
<%}else{ %>

<div class="export">
	<form action="<%=request.getScheme()%>://<%=request.getServerName()%>:<%=request.getServerPort()%>/blog/page/import.jsp" method="post" enctype="multipart/form-data">
		<%for(BlogFile file : BlogManager.getAllBlogFile()){
			long ID = file.getId();
		%>
			
			<div class="file">
				<label>Id:</label>
				<input type="text" name="id" value="<%=ID%>"><br>
				
				<label>Title:</label>
				<input type="text" id="<%=ID%>.title.target" name="<%=ID%>.title">
				<input type="hidden" class="unicode" id="<%=ID%>.title" name="<%=ID%>.title"  value="<%=new String(file.getTitle())%>"><br>
				
				<label>Content:</label>
				<textarea id="<%=ID%>.content.target" rows="1"></textarea>
				<textarea class="unicode" id="<%=ID%>.content" name="<%=ID%>.content" rows="1"><%=new String(file.getContent())%></textarea><br>
				
				<label>Create Date:</label>
				<input type="text" name="<%=ID%>.createDate" value="<%=file.getLastModifiedDate()%>"><br>
				
				<label>Last Modified Date:</label>
				<input type="text" name="<%=ID%>.lastModifiedDate" value="<%=file.getLastModifiedDate()%>"><br>
				
				<label>MimeType:</label>
				<input type="text" name="<%=ID%>.mimeType" value="<%=file.getMimeType()%>"><br>
				
			</div>
		<%} %>
		<input type="hidden" name="method" value="recevieBlog"/>
		<input type="hidden" name="posturl" value="/blog/page/home.jsp"/><br>
		<div class="upload">
			<label>URL:</label><input id="url_upload" type="text" value="<%=request.getScheme()%>://<%=request.getServerName()%>:<%=request.getServerPort()%>/blog/page/import.jsp">
			<input type="submit" value="Upload"/>
		</div>
	</form>
</div>
<%} %>

<script type="text/javascript">
	new Common().convertUnicodeClassElementToTargetElement(document);
</script>

</body>
</html>